package com.zany.util.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.system.vo.UserInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JSONArray;
import com.zany.util.json.JSONException;
import com.zany.util.json.JSONObject;
import com.zany.util.service.GetUserByOrgService;

public class UserByOrgAction extends AbstractActionSupport{

	private static final long serialVersionUID = -8183005591451223814L;
	private static Log logger = LogFactory.getLog(UserByOrgAction.class);
	private GetUserByOrgService getUserByOrgService;
	private List<UserInfoVO> userList = new ArrayList<UserInfoVO>();
	private UserInfoVO userInfoVO = new UserInfoVO();
	
	public void getUser() throws JSONException, ZanyException{
		if(logger.isInfoEnabled()){logger.info("GetUserByOrgAction.getUser()");}
		response = ServletActionContext.getResponse();
		userInfoVO.setUserOrg(request.getParameter("orgId"));
		userList = getUserByOrgService.getUserByOrg(userInfoVO);
		JSONArray listJson = new JSONArray();
		try{
			for(UserInfoVO vo:userList){
				JSONObject json = new JSONObject();
				json.put("key",vo.getUserCode());
				json.put("value",vo.getUserCode()+"-"+vo.getUserName());
				listJson.put(json);
			}
		}catch(JSONException e){
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
		String json = "{user:"+listJson.toString()+"}";
		AjaxTools.exAjax(json,response);
	}
	
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
	}

	public GetUserByOrgService getGetUserByOrgService() {
		return getUserByOrgService;
	}

	public void setGetUserByOrgService(GetUserByOrgService getUserByOrgService) {
		this.getUserByOrgService = getUserByOrgService;
	}
}
